<?php
/**
 * Klasa abstrakcyjna służąca do obsługi połączeń z bazą danych,
 * wyciągania wyników oraz wykonywania zapytań.
 *
 * @author Lukas
 *
 */

include_once 'Zend/Db.php';
include_once 'Zend/Db/Profiler.php';
include_once 'Zend/Db/Table.php';
include_once 'Zend/Db/Adapter/Pdo/Mysql.php';

class DBManager {

  static $zend_db_init = false;
  static $adapters = array();

  public static function initZendDB()
  {
    if (self::$zend_db_init) {
      return;
    }

    $dbc = Config::get('db');
    
    foreach($dbc as $config_name => $db)
    {
         $name = $config_name;
         if((boolean)$db->default)
         {
             $name = 'default';
         }
         self::$adapters[$name] = Zend_Db::factory( $db->adapter, $db->toArray() );

         self::$adapters[$name]->setFetchMode(Zend_Db::FETCH_ASSOC );         
         
         if ($db->profiler) {
            self::$adapters[$name]->getProfiler()->setEnabled(true);
         }
         
         if (!empty($db->charset)) {
            self::$adapters[$name]->getConnection()->exec( "SET NAMES ".$db->charset );
         }
         
         if((boolean)$db->default)
         {
             Zend_Db_Table::setDefaultAdapter( self::$adapters[$name] );
         }
    }

    self::$zend_db_init = true;
  }

  static public function getArrayData( $query, $bind = array(),$adapter="default")
  {
    $result_array = array();
    
    try {
      $db = self::getAdapter($adapter);//Zend_Db_Table::getDefaultAdapter();

      $result_array = $db->fetchAll($query, $bind);
      
      if (empty($result_array)) {
        $result_array = array();
      }
    }
    catch(Zend_Db_Statement_Exception $e) {
      Log::logError(sprintf("Blad zapytania : \r\n%s\r\n%s", $query, $e));
      throw new Exception( $e->getMessage() );
    }
    
    return $result_array;
  }

  static public function getRawData( $query, $bind = array(),$adapter="default")
  {
    $result_array = array();

    try {
      $db = self::getAdapter($adapter);//Zend_Db_Table::getDefaultAdapter();
      $result_array = $db->fetchRow($query, $bind);

      if (empty($result_array)) {
        $result_array = array();
      }
    }
    catch(Zend_Db_Statement_Exception $e) {
      Log::logError(sprintf("Blad zapytania : \r\n%s\r\n%s", $query, $e));
      throw new Exception( $e->getMessage() );
    }

    return $result_array;
  }

  static public function getOneData( $query, $bind = array(),$adapter="default")
  {
    $result = false;

    try {
      $db = self::getAdapter($adapter);//Zend_Db_Table::getDefaultAdapter();
      $result = $db->fetchOne($query, $bind);
    }
    catch(Zend_Db_Statement_Exception $e) {
      Log::logError(sprintf("Blad zapytania : \r\n%s\r\n%s", $query, $e));
      throw new Exception( $e->getMessage() );
    }

    return $result;
  }

  static public function executeUpdateInsert( $query, $bind = array(),$adapter="default")
  {
    $result = false;

    try {
      $db = self::getAdapter($adapter);//Zend_Db_Table::getDefaultAdapter();
      $result = $db->query($query, $bind);
    }
    catch(Zend_Db_Statement_Exception $e) {
      Log::logError(sprintf("Blad zapytania : \r\n%s\r\n%s", $query, $e));
      //throw new Exception( $e->getMessage() );
    }
    return $result;
  }

  static public function insert( $table, $bind = array(),$adapter="default"){
      $result = false;

      try{
          $db = self::getAdapter($adapter);//Zend_Db_Table::getDefaultAdapter();
          if($db->insert($table, $bind)>0){
            return $db->lastInsertId($table);
          }else{
            return false;
          }
      }catch(Zend_Db_Statement_Exception $e) {
          Log::logError(sprintf("Blad zapytania : \r\n%s\r\n%s", $table, $e));
          throw new Exception( $e->getMessage() );
      }
    return $result;
  }

  static public function update($table ,$where = "id=0", $bind = array(),$adapter="default"){
      $result = false;

      try{
          $db = self::getAdapter($adapter);//Zend_Db_Table::getDefaultAdapter();
          return $db->update($table, $bind, $where);
      }catch(Zend_Db_Statement_Exception $e) {
          Log::logError(sprintf("Blad zapytania : \r\n%s\r\n%s", $table, $e));
          throw new Exception( $e->getMessage() );
      }
    return $result;
  }

  static public function delete($table ,$where = "id=0",$adapter="default"){
      $result = false;

      try{
          $db = self::getAdapter($adapter);//Zend_Db_Table::getDefaultAdapter();
          return $db->delete($table, $where);
      }catch(Zend_Db_Statement_Exception $e) {
          Log::logError(sprintf("Blad zapytania : \r\n%s\r\n%s", $table, $e));
          throw new Exception( $e->getMessage() );
      }
    return $result;
  }

  static public function getSelectObject($adapter="default"){
    $db = self::getAdapter($adapter);//Zend_Db_Table::getDefaultAdapter();
    return $db->select();
  }
  
  /**
   *
   * @param string $adapter
   * @return Zend_Db_Adapter_Abstract 
   */
  static private function getAdapter($adapter)
  {
      $db = null;
      if ($adapter === 'default' || !isset(self::$adapters[$adapter]))
      {
          $db = Zend_Db_Table::getDefaultAdapter();
      }
      else
      {
          $db = self::$adapters[$adapter];
      }
      return $db;
  }

}
